//
//  MeViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/3/5.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "MeViewController.h"
#import "MeTableViewCell.h"
#import "FriendTableViewCell.h"
#import "UIButton+LLZ.h"

#import "PrivateKeyViewController.h"
#import "WalletViewController.h"
#import "EditInfoViewController.h"
#import "FollowViewController.h"
#import "FansViewController.h"
#import "VisitViewController.h"
#import "CollectionViewController.h"
#import "MoneyDetailViewController.h"
#import "SettingViewController.h"

@interface MeViewController ()<UITableViewDelegate, UITableViewDataSource>{
    
    NSArray *meArray;///列表里的文字数组
    NSArray *meImageArray;///列表里的图标数组
    
}

@property(nonatomic, strong)UITableView *tableview;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    
    meArray = @[@"密钥", @"明细", @"钱包", @"收藏", @"设置"];
    meImageArray = @[@"密码32.png", @"点赞32.png", @"钱包32.png", @"收藏32.png", @"设置32.png"];
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.llz_width, self.view.llz_height) style:UITableViewStyleGrouped];
    _tableview.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    _tableview.sectionFooterHeight = 1;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _tableview.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:_tableview];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(invokeEditNotification:) name:@"EditNotification" object:nil];
}

#pragma mark - *******tableview DataSource*******
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else if (section == 1) {
        return 4;
    }else return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MeTableViewCell *meCell = [[MeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"meCell"];
            return meCell;//返回个人信息cell
        }
        if (indexPath.row == 1) {
            FriendTableViewCell *friendCell = [[FriendTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
            __weak typeof(self) weakSelf = self;
            [friendCell.following addHander:^{
                FollowViewController *follow = [[FollowViewController alloc]init];
                follow.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:follow animated:YES];
            }];
            [friendCell.follower addHander:^{
                FansViewController *fans = [[FansViewController alloc]init];
                fans.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:fans animated:YES];
            }];
            [friendCell.postNumber addHander:^{
                VisitViewController *visit = [[VisitViewController alloc]init];
                visit.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:visit animated:YES];
            }];
            friendCell.backgroundColor = BACKGROUND_COLOR;
            return friendCell;//返回社交栏的cell
        }

    }
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    if (indexPath.section == 1){
        cell.textLabel.text = meArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:meImageArray[indexPath.row]];
    }
    
    if (indexPath.section == 2){
        cell.textLabel.text = meArray[indexPath.row + 4];
        cell.imageView.image = [UIImage imageNamed:meImageArray[indexPath.row + 4]];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor = BACKGROUND_COLOR;
    return cell;//返回常用cell
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

#pragma mark - *******tableview Delegate*******
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return indexPath.row == 0 ? 120 : 70;
    }else{
        return 50;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
    PrivateKeyViewController *pKey = [[PrivateKeyViewController alloc]init];
    WalletViewController *wallet = [[WalletViewController alloc]init];
    EditInfoViewController *editInfo = [[EditInfoViewController alloc]initWithNibName:@"EditInfoViewController" bundle:[NSBundle mainBundle]];
    CollectionViewController *collect = [[CollectionViewController alloc]init];
    MoneyDetailViewController *mDetail = [[MoneyDetailViewController alloc]init];
    SettingViewController *set = [[SettingViewController alloc]init];
    
    pKey.hidesBottomBarWhenPushed = YES;
    wallet.hidesBottomBarWhenPushed = YES;
    editInfo.hidesBottomBarWhenPushed = YES;
    collect.hidesBottomBarWhenPushed = YES;
    mDetail.hidesBottomBarWhenPushed = YES;
    set.hidesBottomBarWhenPushed = YES;
    
    switch (indexPath.section * 10 + indexPath.row) {
        case 0:
            [self.navigationController pushViewController:editInfo animated:YES];
            break;
        case 10://密钥
            [self.navigationController pushViewController:pKey animated:YES];
            break;
        case 11://密钥
            [self.navigationController pushViewController:mDetail animated:YES];
            break;
        case 12://钱包
            [self.navigationController pushViewController:wallet animated:YES];
            break;
        case 13://收藏
            [self.navigationController pushViewController:collect animated:YES];
            break;
        case 20://设置
            [self.navigationController pushViewController:set animated:YES];
            break;
            
        default:
            break;
    }
}

#pragma mark - *******私有方法*******
/// EditNotification 回调
- (void)invokeEditNotification:(NSNotification *)noti{
    NSDictionary *dic = noti.object;
    MeTableViewCell *cell = [_tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.myImageView.image = dic[@"avator"];
    cell.name.text = dic[@"name"];
    cell.breif.text =  dic[@"introduce"];
}

@end
